const {
    Mysql
} = require('../tools/utils');
const {
    checkOrder
} = require('../tools/checkOrder')
module.exports = async (ctx) => {
    let {
        decodeStr
    } = ctx.request.body;
    decodeStr = decodeStr.replace(/\&/g, "A").replace(/\%/g, "B");
    decodeStr = Buffer.from(decodeStr, 'base64').toString('utf-8');
    let userInfo = JSON.parse(decodeStr);
    let {
        uid,
        startTime,
        endTime,
        isVip,
        orderId,
        openId,
        payAmount
    } = userInfo;
    // 先判断该会员用户是否存在
    let sql1 = `SELECT * from vip where uid = ${uid}`;
    if (uid && startTime && endTime && orderId && payAmount) {
        try {
            let res = await checkOrder(orderId);
            if (res.trade_state == 'SUCCESS') {
                let VIPSQL = `INSERT INTO orderList(uid,orderId,money,type) VALUES(${uid},'${orderId}',${payAmount},'VIP')`;
                try {
                    await Mysql(VIPSQL);
                    let res1 = await Mysql(sql1);
                    if (res1.length) {
                        let now = Date.now();
                        let oldEndTime = res1[0].endTime;
                        // let oldStartTime = res1[0].startTime;
                        let difference = oldEndTime - now;
                        let newEndTime = difference > 0 ? endTime + difference : endTime;
                        let sql2 = `REPLACE INTO vip(uid,startTime,endTime,isVip,orderId,money) VALUES (${uid},${startTime},${newEndTime},${Number(uid)},'${String(orderId)}',${payAmount})`;
                        try {
                            let res = await Mysql(sql2);
                            if (res) {
                                ctx.state.data = {
                                    code: '001'
                                }
                            }
                        } catch (error) {
                            ctx.state.data = {
                                code: '002'
                            }
                        }
                    } else {
                        try {
                            let sql3 = `REPLACE INTO vip(uid,startTime,endTime,isVip,orderId,money) VALUES (${uid},${startTime},${endTime},${Number(uid)},'${String(orderId)}',${payAmount})`
                            let res = await Mysql(sql3);
                            if (res) {
                                ctx.state.data = {
                                    code: '001'
                                }
                            }
                        } catch (error) {
                            ctx.state.data = {
                                code: '002'
                            }
                        }
                    }
                } catch (error) {
                    ctx.state.data = {
                        code: '002'
                    }
                }
            } else {
                ctx.state.data = {
                    code: '002'
                }
            }
        } catch (error) {
            ctx.state.data = {
                code: '002'
            }
        }
    }

}